package org.mvnsearch.rsocket.requester;

import java.time.Duration;
import java.util.function.Function;

import org.mvnsearch.account.Account;
import org.mvnsearch.account.AccountService;
import org.mvnsearch.protobuf.AccountInfoPoto.AccountInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * account controller
 *
 * @author linux_china
 */
@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @GetMapping("/{id}")
    public Mono<Account> show(@PathVariable Integer id) {
        try {
    	Mono<Account> account = accountService.findById(id);
    	return account;
        }catch(RuntimeException ex) {
        	ex.printStackTrace();
        	throw ex;
        }
    }
    @GetMapping("/findAlls")
    public Flux<Account> findAlls(){
    	 Flux<AccountInfo> findAll = accountService.findAll();
    	 Duration timeout=null;
    	Function<Flux<AccountInfo>,Flux<Account>> transformer=mapper->{
			return mapper.map(item->{
    			Account account = new Account();
    			BeanUtils.copyProperties(item, account);
    			return account;
    		});
    	};
		return findAll.transformDeferred(transformer);
    }
}
